<?php

namespace App\Repositories\Eloquent;

use App\Models\RulePolicy;
use App\Repositories\Contracts\RulePolicyInterface;
use MakeRep\Repository;

class RulePolicyRepository extends Repository implements RulePolicyInterface
{

    public $fillable = ['id','admin_id','policy_id','policy_code','platform','airline','way','flight_date','sale_date','cabin','status','commision'];

    public $view = 'rulepolicy';

    public $withs = ['rule_policy_flight'];

    function model()
    {
        return RulePolicy::class;
    }

    /**
     * 规则运价上传
     * @return mixed
     */
    public function create($param){

        //平台上传
        switch ($param['platform']) {

            case 20:
            case 21:
            {

                $specialPolicyInterface = resolve('App\Repositories\Ota\Ctrip\Policy\Contracts\CtripRulePolicyInterface');
                if ($param['platform'] == 20) {
                    $uploadRes = $specialPolicyInterface->add([$param]);
                } else {
                    $uploadRes = $specialPolicyInterface->addMh([$param]);
                }


                if ($uploadRes) {

                    if (isset($uploadRes['TradeResult']) && isset($uploadRes['TradeResult']['Result']) && isset($uploadRes['TradeResult']['Result']['SuccessList'])) {

                        //成功的话加入政策列表
                        $this->savePolicy($param, $uploadRes['TradeResult']['Result']['SuccessList'][0]);
                        return true;
                    } else {

                        if ($uploadRes['ResponseStatus'] && !empty($uploadRes['ResponseStatus']['Errors'])) {

                            return $uploadRes['ResponseStatus']['Errors'][0]['Message'];
                        }
                        return $uploadRes['TradeResult']['ErrorList'][0]['ErrorMessage'];
                    }


                } else {

                    return '网络有问题，稍后重试';
                }

            }
        }

    }

    /**
     * 政策详情
     * @param $param
     * @return mixed
     */
    public function policyInfo($param){

        $info = $this->findById($param['id']);
        if ($info) {

            $data = [
                $info['policy_id']
            ];

            $specialPolicyInterface = resolve('App\Repositories\Ota\Ctrip\Policy\Contracts\CtripRulePolicyInterface');
            if ($info['platform'] == 20) {
                $policyInfo = $specialPolicyInterface->detailSearch($data);
            }else {
                $policyInfo = $specialPolicyInterface->detailMhSearch($data);
            }

            if ($policyInfo && isset($policyInfo['TradeResult']) && isset($policyInfo['TradeResult']['PolicyList'])) {

                return $policyInfo['TradeResult']['PolicyList'];
            }

            return '网络有问题，稍后重试';
        }

        return '未找到该条政策';
    }

    /**
     * 政策更新
     * @param $param
     * @return mixed
     */
    public function ajaxUpdate($param){

        //平台上传
        switch ($param['platform']){

            case 20:
            case 21: {

                $specialPolicyInterface = resolve('App\Repositories\Ota\Ctrip\Policy\Contracts\CtripRulePolicyInterface');
                if ($param['platform'] == 10) {
                    $uploadRes = $specialPolicyInterface->change([$param]);
                } else {
                    $uploadRes = $specialPolicyInterface->changeMh([$param]);
                }


                if ($uploadRes){

                    if (isset($uploadRes['TradeResult']) && isset($uploadRes['TradeResult']['Result']) && isset($uploadRes['TradeResult']['Result']['SuccessList'])) {

                        //成功的话加入政策列表
                        $this->savePolicy($param,$param['PolicyID']);
                        return true;
                    } else {

                        if ($uploadRes['ResponseStatus'] && !empty($uploadRes['ResponseStatus']['Errors']) ) {

                            return $uploadRes['ResponseStatus']['Errors'][0]['Message'];
                        }
                        return $uploadRes['TradeResult']['ErrorList'][0]['ErrorMessage'];
                    }

                } else {

                    return '网络有问题，稍后重试';
                }

            }
        }
    }

    /**
     * 简易更新政策
     * @param $param
     * @return mixed
     */
    public function itemUpdate($param){

        //平台上传
        $policy = $this->findById($param['id']);
        if ($policy) {

            switch ($policy['platform']){

                case 20:
                case 21: {

                    if (!empty($param['status'])) {

                        $param['IsValid'] = $param['status'] == 1 ? 'T' : 'F';
                    }

                    //处理数据
                    $specialPolicyInterface = resolve('App\Repositories\Ota\Ctrip\Policy\Contracts\CtripRulePolicyInterface');
                    if ($policy['platform'] == 10) {
                        $uploadRes = $specialPolicyInterface->changePrice([$param]);
                    } else{
                        $uploadRes = $specialPolicyInterface->changeMhPrice([$param]);
                    }


                    if ($uploadRes && isset($uploadRes['TradeResult'])){
                        if (isset($uploadRes['TradeResult']['Result']) && isset($uploadRes['TradeResult']['Result']['SuccessList']) && !empty($uploadRes['TradeResult']['Result']['SuccessList'])) {

                            //成功的话加入政策列表
                            $this->updatePolicy($param);
                            return true;
                        } else {
                            logger($param);
                            return $uploadRes['TradeResult']['ErrorList'][0]['ErrorMessage'];
                        }

                    } else {

                        return '网络有问题，稍后重试';
                    }

                }
            }
        }

    }

    /**
     * 政策删除
     * @param $param
     * @return mixed
     */
    public function deletePolicy($param){

        //平台上传
        if (in_array($param['platform'],$param['admin_platform'])) {
            switch ($param['platform']) {

                case 20:
                case 21:
                {

                    $policy = [
                        $param['PolicyID'],
                    ];

                    //处理数据
                    $specialPolicyInterface = resolve('App\Repositories\Ota\Ctrip\Policy\Contracts\CtripRulePolicyInterface');
                    if ($param['platform'] == 20) {
                        $uploadRes = $specialPolicyInterface->delete($policy);
                    } else {
                        $uploadRes = $specialPolicyInterface->deleteMh($policy);
                    }
                    if ($uploadRes && isset($uploadRes['TradeResult'])) {

                        if (isset($uploadRes['TradeResult']['Result']) && isset($uploadRes['TradeResult']['Result']['SuccessList'])) {

                            $this->delete(['id' => $param['id']]);

                            return true;
                        }

                        return $uploadRes['TradeResult']['ErrorList'] ? $uploadRes['TradeResult']['ErrorList'][0]['ErrorMessage'] : '删除失败：政策ID：' . $uploadRes['TradeResult']['Result']['FailedList'][0];
                    } else {

                        return '网络有问题，稍后重试';
                    }
                }
            }
        }

        return '你无权访问，请联系管理员';
    }

    /**
     * 保存政策
     * @param $param
     * @param $policyId
     * @return void
     */
    public function savePolicy($param,$policyId){

        switch ($param['platform']){
            case 20:
            case 21: {
                $data = [
                    'admin_id'              => $param['admin']['id'],
                    'policy_id'             => $policyId,
                    'policy_code'           => $param['PolicyCode'],
                    'platform'              => $param['platform'],
                    'airline'               => $param['AirLine'],
                    'way'                   => $param['FlightWay'] == 'S' ? 1 : 2,
                    'flight_date'           => $param['GoEffectDate'].'/'.$param['GoExpiryDate'],
                    'sale_date'             => $param['SaleEffectDate'].'/'.$param['SaleExpiryDate'],
                    'commision'             => $param['RulePriceList'][0]['Commision'],
                    'cabin'                 => $param['RulePriceList'][0]['SubClass'],
                    'status'                => isset($param['PolicyStatus']) &&  $param['PolicyStatus'] == 'NORMAL' ? 1 : 2,
                ];

                break;
            }
        }

        $rulePolicyInterface = resolve('App\Repositories\Contracts\RulePolicyFlightInterface');

        if (empty($param['id'])) {

            $res = $this->add($data);
            $rulePolicyInterface->create($param,$res['id']);
        } else {

            $res = $this->update(['id'=>$param['id']],$data);
            if ($res) {

                $rulePolicyInterface->create($param,$param['id']);
            }
        }

    }

    /**
     * 更新本地政策
     * @param $param
     * @return void
     */
    private function updatePolicy($param){

        $data = [
            'status'    => $param['status']
        ];

        $this->update(['id'=>$param['id']],$data);
    }
}